package com.chaos.eurekaproducer.leetCode.sort;

/**
 * @author liaopeng
 * @title: InsertSort
 * @projectName eureka-producer
 * @description: 插入排序
 * @date 2021/7/5下午4:47
 */
public class InsertSort {
    public static int[] insertSort(int[] a){
        if (a.length==0){
            return a;
        }
        for (int i = 1; i < a.length; ++i) {
            int value = a[i];
            int j = i-1;
            for (; j >=0 ; j--) {
                if (value<a[j]){
                    a[j+1] = a[j];
                }else{
                    break;
                }
            }
            a[j+1]=value;
        }
        return a;
    }

    public static void main(String[] args) {
        int[] a = new int[]{3,4,6,1,2};
        int[] b = insertSort(a);
        for (int i = 0; i < b.length; i++) {
            int i1 = b[i];
            System.out.println(i1);
        }
    }
}
